using System;
using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFrameworkLibraly.Controls
{
    public class ButtonControl:Control
    {
        /// <summary>
        /// Font that is used to print text on the button
        /// </summary>
        public SpriteFont SpriteFont { get; set; }
        
        /// <summary>
        /// Text that is printed on the button
        /// </summary>
        public virtual string Text { get; set; }

        /// <summary>
        /// Texture that is used for background when the buttong is pressed
        /// </summary>
        public Texture2D BackgroundTexturePressed { get; set; }

        
        private Vector2 _textPosition;

        /// <summary>
        /// Creates a new instance of ButtonControl
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="destinationRectangle">Where the manipulator will be located</param>
        /// <param name="text">Text that will be printed</param>
        /// <param name="spriteFont">Font that will be used to print the text</param>
        /// <param name="backGroundTexture">Texture that will be drawn as a background</param>
        /// <param name="backGroundTexturePressed">Texture that will be drawn as a background when button is pressed</param>
        /// <param name="foreColor">Color of the text</param>
        public ButtonControl(MyGame game, Rectangle destinationRectangle, string text, SpriteFont spriteFont, Texture2D backGroundTexture, Texture2D backGroundTexturePressed, Color foreColor):base(game)
        {
            DestinationRectangle = destinationRectangle;
            Text = text;
            SpriteFont = spriteFont;
            BackgroundTexture = backGroundTexture;
            BackgroundTexturePressed = backGroundTexturePressed;
            ForeColor = foreColor;
        }
        


        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if(!Visible)
                return;
            if (IsPressed)
            {
                if (BackgroundTexturePressed != null)
                {
                    spriteBatch.Draw(BackgroundTexturePressed, DestinationRectangle, BackColorFilter);
                }
            }
            else if (BackgroundTexture != null)
            {
                spriteBatch.Draw(BackgroundTexture, DestinationRectangle, BackColorFilter);
            }

            if (SpriteFont != null && !String.IsNullOrEmpty(Text))
                spriteBatch.DrawString(SpriteFont, Text, _textPosition, ForeColor);
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if(!Enabled)
                return;
            if (SpriteFont != null && !string.IsNullOrEmpty(Text))
            {
                Vector2 textSize = SpriteFont.MeasureString(Text);
                _textPosition = new Vector2((int)(DestinationRectangle.Left + (DestinationRectangle.Width - textSize.X) / 2),
                                            (int)(DestinationRectangle.Top + (DestinationRectangle.Height - textSize.Y) / 2));
            }
        }

    }
}
